package com.hebeu.gfh.controller;

import com.hebeu.gfh.pojo.po.ClubMember;
import com.hebeu.gfh.pojo.po.Clubs;
import com.hebeu.gfh.pojo.po.User;
import com.hebeu.gfh.pojo.vo.clubIdAndrole;
import com.hebeu.gfh.pojo.vo.userIdAndClubId;
import com.hebeu.gfh.service.clubMemberService;
import com.hebeu.gfh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/clubMember")
public class clubMemberController {
    @Autowired
    clubMemberService clubMemberService;

    //根据社团id查询社团成员
    @PostMapping("/findClubMember")
    public R findClubMember(clubIdAndrole clubIdAndrole){
        List<User> userList = clubMemberService.findClubMember(clubIdAndrole);
        return R.ok(userList);
    }

    //加入社团
    @PostMapping("/joinClub")
    public R joinClub(ClubMember clubMember){
        try {
        clubMemberService.joinClub(clubMember);
        }catch (Exception e){
            return R.error(500,"加入社团失败");
        }
        return R.ok("加入社团成功");
    }
    //退出社团
    @PostMapping("/exitClub")
    public R exitClub(userIdAndClubId userIdAndClubId){
    Integer memberId =clubMemberService.findmemberId(userIdAndClubId);
        try {
            clubMemberService.exitClub(memberId);
        }catch (Exception e){
            return R.error(500,"退出社团失败");
        }
        return R.ok("退出社团成功");
    }
    //根据userid查询社团
    @PostMapping("/findClubsListByuserId")
    public R findClubsListByuserId(int userId){
        List<Clubs> clubsList = clubMemberService.findClubsListByuserId(userId);
        return R.ok(clubsList);
    }
    //根据userid和clubid修改roleinclub
    @PostMapping("/updateroleinclub")
    public R updateroleinclub(userIdAndClubId userIdAndClubId){
        try {
            clubMemberService.updateroleinclub(userIdAndClubId);
        }catch (Exception e){
            System.out.println(e);
            return R.error(500,"修改失败");
        }
        return R.ok("修改成功");
    }
    //根据userid和clubid查询是否加入该社团
    @PostMapping("/inclub")
    public R inclub(userIdAndClubId userIdAndClubId){
        Integer memberId =clubMemberService.findmemberId(userIdAndClubId);
        return R.ok(memberId);
    }

    @PostMapping("/getClubMember")
    public R getClubMember(int clubId){
        System.out.println(clubId);
        List<User> userList = new ArrayList<>();
        try {
            userList  =   clubMemberService.getClubMember(clubId);
        }catch (Exception e){
            System.out.println(e);
            System.out.println("---------------------");
        }

      return R.ok(userList);
    }
}
